package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.bean.ComBean;
import com.kinon.norogamilk.http.CommonSubscriber;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.RxBus;
import com.kinon.norogamilk.util.RxUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by lhqq on 2017-12-18.
 */

public class HeatingActivity extends BaseActivity implements View.OnClickListener {
    protected CompositeDisposable mCompositeDisposable;
    private Button btn_back;
    private int msign = 0;
    private Handler myHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            intent();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heating_layout);
        initVew();
//        myHandler.postDelayed(mRunnable,5000);
    }

    private void initVew() {
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        Log.e(TAG, "下发微波炉电源开关控制------ ");
        sendPortData(sendHeatString(0x8F, 0x10));
        newDataReceived();
    }

    private void newDataReceived() {
        mCompositeDisposable = new CompositeDisposable();
        Disposable dis = RxBus.getDefault().toFlowable(ComBean.class)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<ComBean>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<ComBean>() {
                    @Override
                    public void onFail(Throwable t, int code, String msg) {
                    }

                    @Override
                    public void onNext(final ComBean comBean) {
//                        if(totalNum<0){
//                            setout();
//                            return;
//                        }
//                        Log.e(TAG, "i== "+(++i) );
//                        Log.e(TAG, "ByteArrToHex =="+MyFunc.ByteArrToHex(comBean.getbRec()));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                if (comBean.getbRec().length > 6) {

//                                    Log.e(TAG, "getbRec =="+comBean.getbRec()[5]);
                                    if (comBean.getbRec()[5] == -113) { //微波炉开关控制返回
                                        if (comBean.getbRec().length > 9) {
                                            msign = 0;

                                            if (comBean.getbRec()[8] == 1) { //电源开启
                                                Log.e(TAG, "电源开启===主板轮询------ ");
                                                sendPortData(sendString(0x02));
                                            } else { //电源关闭
                                                Log.e(TAG, "电源关闭===主板轮询------ ");
                                                sendPortData(sendString(0x02));
                                            }
                                        } else {   //微波炉轮询失败
                                            Log.e(TAG, "微波炉轮询失败==开启微波炉电源------ ");
                                            sendPortData(sendHeatString(0x8F, 0x10));
                                        }

                                    } else if (comBean.getbRec()[5] == 2) {  //主板轮询返回
                                        if (comBean.getbRec().length > 52) {
                                            if (comBean.getbRec()[52] == 1 && msign == 0) { //01电源接通
                                                msign = 1;
                                                Log.e(TAG, "电源接通===主板轮询------ ");
                                                sendPortData(sendString(0x02));


                                            } else if (comBean.getbRec()[52] == 01) { //01电源接通是否开始工作
                                                if (comBean.getbRec()[56] == 01) { //开始加热
                                                    Log.e(TAG, "开始加热==加热中主板轮询------ ");
                                                    sendPortData(sendString(0x02));
                                                } else { //未开始加热
                                                    Log.e(TAG, "未开始加热------ ");
                                                    sendPortData(sendString(0x02));
                                                }
                                            } else if (comBean.getbRec()[52] == 0 && msign == 1) { //加热完成后电源从接通到端口
                                                Log.e(TAG, "加热完成后电源从接通到端口------ ");
                                                intent();
                                            } else {//电源未接通

                                                msign = 0;
                                                Log.e(TAG, "电源未接通==主板轮询------ ");
                                                sendPortData(sendString(0x02));
                                            }

                                        } else {
                                            Log.e(TAG, "主板轮询出错==重新轮询------ ");
                                            sendPortData(sendString(0x02));
                                        }
                                    }

                                } else { //返回长度少于6的时候
                                    Log.e(TAG, "返回长度少于6的时候主板轮询----");
                                    sendPortData(sendHeatString(0x8F, 0x10));
                                }

                            }
                        });
                    }
                });
        mCompositeDisposable.add(dis);

    }

    private void intent() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
        Intent intent = new Intent(HeatingActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                intent();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }
}
